package com.grm.sys.controller;

import com.grm.common.entity.Result;
import com.grm.common.annotation.HasPerm;
import com.grm.common.annotation.Log;
import com.grm.sys.model.SysPerm;
import com.grm.sys.request.perm.SysPermAddParam;
import com.grm.sys.request.perm.SysPermUpdateParam;
import com.grm.sys.response.SysPermResponse;
import com.grm.sys.service.SysPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/perm")
public class SysPermController {
    @Autowired
    private SysPermService sysPermService;

    @GetMapping("/query/tree/all")
    public Result queryTreeAll() {
        List<SysPermResponse> tree = sysPermService.queryAllTree();
        return Result.success(tree);
    }

    @GetMapping("/query/tree/menu")
    public Result queryTreeMenu() {
        List<SysPermResponse> tree = sysPermService.queryMenuTree();
        return Result.success(tree);
    }

    @Log("添加权限")
    @PostMapping("/add")
    @HasPerm("sys:perm:add")
    public Result add(@RequestBody SysPermAddParam sysPermAddParam) {
        sysPermService.add(sysPermAddParam);
        return Result.success();
    }

    @Log("修改权限")
    @PutMapping("/update")
    @HasPerm("sys:perm:update")
    public Result update(@RequestBody SysPermUpdateParam sysPermUpdateParam) {
        sysPermService.update(sysPermUpdateParam);
        return Result.success();
    }

    /**
     * 删除权限
     */
    @Log("删除权限")
    @DeleteMapping("/delete/{id}")
    @HasPerm("sys:perm:delete")
    public Result delete(@PathVariable("id") Long id) {
        sysPermService.delete(id);
        return Result.success();
    }

    /**
     * 查询角色已经拥有的权限
     */
    @GetMapping("/query/permIdsByRoleId/{roleId}")
    public Result queryPermIdsByRoleId(@PathVariable Long roleId) {
        List<Long> permIds = sysPermService.queryPermIdsByRoleId(roleId);
        return Result.success(permIds);
    }
}
